//
//  RDNetworkTool.m
//  eCourseApp
//
//  Created by 飞猪 on 2018/9/2.
//  Copyright © 2018年 Facebook. All rights reserved.
//

#import "RDNetworkTool.h"

@implementation RDNetworkTool

+ (void)requestForChapter:(NSDictionary *)params successBlock:(void(^)(RDNETChapterModel *))successBlock failBlock:(void(^)(NSString *))failBlock {
  
  //1.
  AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
  sessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
  
  //2.
//  @"chId": @"1841624",
//  @"bookId": @"79786"
  NSString *chId = [params objectForKey:@"chId"];
  NSString *bookId = [params objectForKey:@"bookId"];
  if (chId == nil || chId.length <= 0 || bookId == nil || bookId.length <= 0) {
    
    if (failBlock) {
      failBlock(@"不合法的请求参数");
    }
    
    return;
  }
  
  NSString *urlStr = [NSString stringWithFormat:@"http://39.108.151.99/api/server/chaptercontent/queryChContentByApp/%@/%@", bookId, chId];
  [sessionManager GET:urlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    NSError *error = nil;
    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
    
    NSDictionary *RESULT = [jsonDict objectForKey:@"RESULT"];
    if (jsonDict && error == nil) {
      NSInteger RESULT_CODE = [[RESULT objectForKey:@"RESULT_CODE"] integerValue];
      if (RESULT_CODE == 1000) {
        NSDictionary *ROOT = [jsonDict objectForKey:@"ROOT"];
        NSDictionary *ChpterInfo = [ROOT objectForKey:@"ChpterInfo"];
        NSDictionary *Other = [ROOT objectForKey:@"Other"];
        
        RDNETChapterModel *model = [[RDNETChapterModel alloc] init];
        model.chContent = [ChpterInfo objectForKey:@"chContent"];
        model.chId = [[ChpterInfo objectForKey:@"chId"] stringValue];
        model.bookId = [[ChpterInfo objectForKey:@"bookId"] stringValue];
        model.chCharge = [[ChpterInfo objectForKey:@"chCharge"] stringValue];
        model.chName = [ChpterInfo objectForKey:@"chName"];
        
        model.nextChId = [[Other objectForKey:@"nextChId"] stringValue];
        model.preChId = [[Other objectForKey:@"preChId"] stringValue];
        model.nextChCharge = [[Other objectForKey:@"nextChCharge"] stringValue];
        model.preChCharge = [[Other objectForKey:@"preChCharge"] stringValue];
        
        if (successBlock) successBlock(model);
      }
      else {
        NSString *RESULT_DESC = [RESULT objectForKey:@"RESULT_DESC"];
        if (failBlock) failBlock(RESULT_DESC);
      }
    }
    else {
      if (failBlock) failBlock(@"暂无数据");
    }
    
  } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    
    if (failBlock) {
      failBlock(error.localizedDescription);
    }
    
  }];
  
}


// 请求前台配置
+ (void)requestForBookFaceList:(NSDictionary *)params successBlock:(void(^)(NSArray<RABookFaceModel *> *))successBlock failBlock:(void(^)(NSString *))failBlock {
  
  //1.
  AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
  sessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
  
  //2.
  NSString *chId = [params objectForKey:@"id"];
  if (chId == nil || chId.length <= 0) {
    
    if (failBlock) {
      failBlock(@"不合法的请求参数");
    }
    
    return;
  }
  
  NSString *urlStr = [NSString stringWithFormat:@"http://39.108.151.99/api/mobile/index/%@", chId];
  [sessionManager GET:urlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    NSError *error = nil;
    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
    
    NSDictionary *RESULT = [jsonDict objectForKey:@"RESULT"];
    if (jsonDict && error == nil) {
      NSInteger RESULT_CODE = [[RESULT objectForKey:@"RESULT_CODE"] integerValue];
      if (RESULT_CODE == 1000) {
        NSArray *ChpterList = [jsonDict objectForKey:@"ROOT"];
        
        NSMutableArray *tempArr = [NSMutableArray array];
        for (int i = 0; i < ChpterList.count; ++i) {
          NSDictionary *dict = [ChpterList objectAtIndex:i];
          
          RABookFaceModel *model = [[RABookFaceModel alloc] init];
          model.toUrl = [dict objectForKey:@"toUrl"];
          model.templet = [dict objectForKey:@"templet"];
          model.id = [[dict objectForKey:@"id"] stringValue];
          model.iconUrl = [dict objectForKey:@"iconUrl"];
          model.parentId = [[dict objectForKey:@"parentId"] stringValue];
          model.name = [dict objectForKey:@"name"];
          model.children = nil;
          
          NSArray *arr = [dict objectForKey:@"children"];
          
          if (arr && arr.count > 0) {
            NSMutableArray *innerArray = [NSMutableArray array];
            for (int i = 0; i < arr.count; ++i) {
              
              NSDictionary *dict = [arr objectAtIndex:i];
              RABookFaceModel *model = [[RABookFaceModel alloc] init];
              model.toUrl = [dict objectForKey:@"toUrl"];
              model.templet = [dict objectForKey:@"templet"];
              model.id = [[dict objectForKey:@"id"] stringValue];
              model.iconUrl = [dict objectForKey:@"iconUrl"];
              model.parentId = [[dict objectForKey:@"parentId"] stringValue];
              model.name = [dict objectForKey:@"name"];
              model.children = nil;
              
              [innerArray addObject:model];
            }
            
            model.children = innerArray;
          }
          
          [tempArr addObject:model];
        
        }
        
        if (successBlock) successBlock(tempArr.copy);
      }
      else {
        NSString *RESULT_DESC = [RESULT objectForKey:@"RESULT_DESC"];
        if (failBlock) failBlock(RESULT_DESC);
      }
    }
    else {
      if (failBlock) failBlock(@"暂无数据");
    }
    
  } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    
    if (failBlock) {
      failBlock(error.localizedDescription);
    }
    
  }];
  
}

// 请求章节列表数据
+ (void)requestForChapterList:(NSDictionary *)params successBlock:(void(^)(NSArray<RDNETChapterListModel *> *))successBlock failBlock:(void(^)(NSString *))failBlock {
  
  //1.
  AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
  sessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
  
  //2.
  NSString *bookId = [params objectForKey:@"bookId"];
  if (bookId == nil || bookId.length <= 0) {
    
    if (failBlock) {
      failBlock(@"不合法的请求参数");
    }
    
    return;
  }
  
  NSString *urlStr = [NSString stringWithFormat:@"http://39.108.151.99/api/server/chapter/queryChsByApp/%@", bookId];
  [sessionManager GET:urlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    NSError *error = nil;
    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
    
    NSDictionary *RESULT = [jsonDict objectForKey:@"RESULT"];
    if (jsonDict && error == nil) {
      NSInteger RESULT_CODE = [[RESULT objectForKey:@"RESULT_CODE"] integerValue];
      if (RESULT_CODE == 1000) {
        NSDictionary *ROOT = [jsonDict objectForKey:@"ROOT"];
        NSArray *ChpterList = [ROOT objectForKey:@"ChpterList"];
        
        NSMutableArray *tempArr = [NSMutableArray array];
        for (int i = 0; i < ChpterList.count; ++i) {
          NSDictionary *dict = [ChpterList objectAtIndex:i];
          
          RDNETChapterListModel *model = [[RDNETChapterListModel alloc] init];
          model.chId = [[dict objectForKey:@"chId"] stringValue];
          model.bookId = [[dict objectForKey:@"bookId"] stringValue];
          model.chName = [dict objectForKey:@"chName"];
          
          model.nextChId = [[dict objectForKey:@"nextChId"] stringValue];
          model.preChId = [[dict objectForKey:@"preChId"] stringValue];
          model.nextChCharge = [[dict objectForKey:@"nextChCharge"] stringValue];
          model.preChCharge = [[dict objectForKey:@"preChCharge"] stringValue];
          
          [tempArr addObject:model];
          
        }
        
        if (successBlock) successBlock(tempArr.copy);
      }
      else {
        NSString *RESULT_DESC = [RESULT objectForKey:@"RESULT_DESC"];
        if (failBlock) failBlock(RESULT_DESC);
      }
    }
    else {
      if (failBlock) failBlock(@"暂无数据");
    }
    
  } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    
    if (failBlock) {
      failBlock(error.localizedDescription);
    }
    
  }];
  
}

// 请求书本介绍详情
+ (void)requestForBookDetail:(NSDictionary *)params successBlock:(void(^)(RABookDetailModel *))successBlock failBlock:(void(^)(NSString *))failBlock {
  
  //1.
  AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
  sessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
  
  //2.
  NSString *bookId = [params objectForKey:@"bookId"];
  if (bookId == nil || bookId.length <= 0) {
    
    if (failBlock) {
      failBlock(@"不合法的请求参数");
    }
    
    return;
  }
  
  NSString *urlStr = [NSString stringWithFormat:@"http://39.108.151.99/api/server/article/queryArtByApp/%@", bookId];
  [sessionManager GET:urlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    NSError *error = nil;
    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
    
    NSDictionary *RESULT = [jsonDict objectForKey:@"RESULT"];
    if (jsonDict && error == nil) {
      NSInteger RESULT_CODE = [[RESULT objectForKey:@"RESULT_CODE"] integerValue];
      if (RESULT_CODE == 1000) {
        NSDictionary *ROOT = [jsonDict objectForKey:@"ROOT"];
        NSDictionary *dict = [ROOT objectForKey:@"bookInfo"];
        
        RABookDetailModel *model = [[RABookDetailModel alloc] init];
        model.bookCover = [dict objectForKey:@"bookCover"];
        model.bookId = [[dict objectForKey:@"bookId"] stringValue];
        model.bookName = [dict objectForKey:@"bookName"];
        model.bookAuthor = [dict objectForKey:@"bookAuthor"];
        
        model.status = [dict objectForKey:@"status"];
        model.words = [[dict objectForKey:@"words"] stringValue];
        model.bookIntro = [dict objectForKey:@"bookIntro"];
        model.bookClass = [dict objectForKey:@"bookClass"];
        model.categoryId = [dict objectForKey:@"categoryId"];
        model.chapterCount = [[dict objectForKey:@"chapterCount"] stringValue];
        
        if (successBlock) successBlock(model);
      }
      else {
        NSString *RESULT_DESC = [RESULT objectForKey:@"RESULT_DESC"];
        if (failBlock) failBlock(RESULT_DESC);
      }
    }
    else {
      if (failBlock) failBlock(@"暂无数据");
    }
    
  } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    
    if (failBlock) {
      failBlock(error.localizedDescription);
    }
    
  }];
  
}

@end
